Imports Clo = Enhancements.Core.Logging
Imports Cmp = System.Runtime.CompilerServices
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Primitives
    ''' <summary>Contains features that add up to the .Net Framework System.Int16 primitive type.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public Module Int16

        ''' <summary>Determines a value's hexadecimal representation.</summary>
        ''' <param name="_value">Value to represent.</param>
        ''' <exception cref="IsInRangeNotException">_value is lower than 0 or higher than System.Int16.MaxValue.</exception>
        ''' <returns> </returns>
        <Cmp.Extension()> _
        Public Function ToHexadecimalString(ByVal _value As System.Int16) As String
            _value.IsInRange("_value", 0, Sys.Int16.MaxValue, True)

            Dim _returnValue As System.String = Sys.Convert.ToString(_value, 16).ToUpperInvariant()

            Return _returnValue
        End Function

        ''' <summary>Determines a value from it's hexadecimal representation.</summary>
        ''' <param name="_value">Value to inspect.</param>
        ''' <exception cref="IsNullException">_value is null.</exception>
        ''' <exception cref="IsEmptyException">_value is empty.</exception>
        ''' <exception cref="IsInRegularExpressionNotException">_value is not well-formed.</exception>
        ''' <exception cref="IsInRangeNotException">The resulting value is lower than 0 or higher than System.Int16.MaxValue.</exception>
        ''' <returns> </returns>
        Public Function FromHexadecimalString(ByVal _value As System.String) As System.Int16
            _value.IsEmptyNot("_value", True)
            _value.IsInRegularExpression("_value", Ere.Repository.HexadecimalNumber, True)

            Dim _returnValue As System.Int16 = 0
            Try
                _returnValue = Sys.Convert.ToInt16(_value, 16)
            Catch _exception As Sys.OverflowException
                IsInRangeNotException.___Throw("Sys.Convert.ToInt16(_value, 16)", 0, 0, Sys.Int16.MaxValue)
            End Try

            Return _returnValue
        End Function

        ''' <summary>Determines a value's textual representation in Portuguese.</summary>
        ''' <param name="_value">Value to represent.</param>
        ''' <exception cref="IsInRangeNotException">_value must be higher or equal to System.Int16.MinValue + 1.</exception>
        ''' <returns> </returns>
        <Cmp.Extension()> _
        Public Function ToPortugueseString(ByVal _value As System.Int16) As System.String
            _value.IsInRange("_value", Sys.Int16.MinValue + 1, Sys.Int16.MaxValue, True)

            Dim _returnValue As System.String = String.Empty
            Dim _valueIntegerPart As System.Int64 = System.Math.Abs(_value)
            Dim _valueDecimalPart As System.Int64 = 0
            Dim _valueSignal As System.String = String.Empty
            If _value >= 0 Then
                _valueSignal = "+"
            Else
                _valueSignal = "-"
            End If
            _returnValue = Int64.___ToPortugueseString(_valueIntegerPart, _valueDecimalPart, _valueSignal)

            Return _returnValue
        End Function

    End Module
End Namespace
